home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / vmm.lib / vmm_lib.asm < prev    next >
Encoding:
Assembly Source File  |  1995-12-22  |  15.1 KB  |  422 lines

  1.                     SECTION   CODE
  2.  
  3.  
  4.                     INCLUDE   "exec/types.i"
  5.                     INCLUDE   "exec/resident.i"
  6.                     INCLUDE   "exec/memory.i"
  7.                     INCLUDE   "exec/ports.i"
  8.                     INCLUDE   "exec/execbase.i"
  9.                     INCLUDE   "exec/libraries.i"
  10.                     INCLUDE   "exec/alerts.i"
  11.                     INCLUDE   "exec/initializers.i"
  12.                     INCLUDE   "exec/funcdef.i"
  13.                     INCLUDE   "exec/exec_lib.i"
  14.                     INCLUDE   "dos/dostags.i"
  15.                     INCLUDE   "dos/dos_lib.i"
  16.                     INCLUDE   "/shared_defs.i"
  17.                     INCLUDE   "vmm_lib.i"
  18.  
  19.                     MACHINE   68030
  20.  
  21. VERSION             EQU       3
  22. REVISION            EQU       0
  23. MYPRI               EQU       0
  24.  
  25.                     * These two are missing in dos/dos_lib.i
  26.  
  27. _LVONewLoadSeg      EQU       -$300
  28. _LVOCreateNewProc   EQU       -$1f2
  29.  
  30.                     * A library cannot be called directly
  31.  
  32. Start:              moveq     #-1,d0
  33.                     rts
  34.  
  35. RomTag:             dc.w      RTC_MATCHWORD
  36.                     dc.l      RomTag
  37.                     dc.l      EndCode
  38.                     dc.b      RTF_AUTOINIT
  39.                     dc.b      VERSION
  40.                     dc.b      NT_LIBRARY
  41.                     dc.b      MYPRI
  42.                     dc.l      VM_Name
  43.                     dc.l      VM_Id
  44.                     dc.l      Init
  45.  
  46. VM_Name:            LIBNAME
  47. VM_Id:              dc.b      "$VER: vmm.library 3.0 (4.3.95)",0
  48.  
  49. DOSName:            dc.b      "dos.library",0
  50.  
  51. VMPortName          VMPORTNAME
  52. StarterPortName     STARTER_PORT_LIB
  53. ManagerName         VM_MANAGER_NAME
  54. VMMName             PROGPATH
  55.                     ds.l      0
  56.  
  57. VM_ManagerTags      dc.l      NP_Seglist,0
  58.                     dc.l      NP_FreeSeglist,1
  59.                     dc.l      NP_Name,ManagerName
  60.                     dc.l      NP_StackSize,4000
  61.                     dc.l      TAG_DONE,0
  62.  
  63.  
  64. Init:               dc.l      VM_LIB_SIZE
  65.                     dc.l      FuncTable
  66.                     dc.l      DataTable
  67.                     dc.l      InitRoutine
  68.  
  69. FuncTable:          dc.l      Open
  70.                     dc.l      Close
  71.                     dc.l      Expunge
  72.                     dc.l      Null
  73.  
  74.                     dc.l      AllocVMem
  75.                     dc.l      FreeVMem
  76.                     dc.l      AvailVMem
  77.                     dc.l      AllocVVec
  78.                     dc.l      FreeVVec
  79.                     dc.l      -1
  80.  
  81. DataTable:          INITBYTE  LN_TYPE,NT_LIBRARY
  82.                     INITBYTE  LN_PRI,MYPRI
  83.                     INITLONG  LN_NAME,VM_Name
  84.                     INITBYTE  LIB_FLAGS,LIBF_SUMUSED+LIBF_CHANGED
  85.                     INITWORD  LIB_VERSION,VERSION
  86.                     INITWORD  LIB_REVISION,REVISION
  87.                     INITLONG  LIB_IDSTRING,VM_Id
  88.                     dc.l      0
  89.  
  90.  
  91. * The init routine checks, if VMM is running. If it is, it simply requests
  92. * the address of the memory header and the semaphore. If VMM has not been
  93. * started yet, its code is loaded and the VM_Manager created. As soon as the
  94. * VM_Port exists, the required data (as before) is requested.                    
  95.  
  96. InitRoutine:        * Called with:
  97.                     *    Seglist in A0
  98.                     *    Lib ptr in D0
  99.  
  100.                     * Store the seglist and open dos.library
  101.                     move.l    a5,-(sp)
  102.                     move.l    d0,a5
  103.                     move.l    a0,VM_LIB_SEGLIST(a5)
  104.                     moveq     #37,d0
  105.                     lea       DOSName(pc),a1
  106.                     jsr       _LVOOpenLibrary(a6)
  107.                     move.l    d0,VM_DOSLIB(a5)
  108.                     beq       InitFailure
  109.  
  110.                     * install the public starter port
  111.                     jsr       _LVOCreateMsgPort(a6)
  112.                     move.l    d0,VM_STARTERPORT(a5)
  113.                     beq       InitFailure
  114.                     move.l    d0,a1
  115.                     lea       StarterPortName(pc),a0
  116.                     move.l    a0,LN_NAME(a1)
  117.                     clr.b     LN_PRI(a1)
  118.                     jsr       _LVOAddPort(a6)
  119.  
  120.                     * Check if VMM is already running
  121.                     lea       VMPortName(pc),a1
  122.                     jsr       _LVOFindPort(a6)
  123.                     move.l    d0,VM_PORT(a5)
  124.                     bne       FoundPort
  125.  
  126. * VMM is not running yet. Load the code and start the VM_Manager
  127.                     moveq     #VMM_SIZE,d0
  128.                     move.l    #MEMF_PUBLIC|MEMF_CLEAR,d1
  129.                     jsr       _LVOAllocMem(a6)
  130.                     move.l    d0,VM_INIT_MSG(a5)
  131.                     beq       InitFailure
  132.                     move.l    VM_DOSLIB(a5),a6
  133.                     lea       VMMName(pc),a0
  134.                     move.l    d2,-(sp)
  135.                     move.l    a0,d1
  136.                     moveq     #0,d2
  137.                     jsr       _LVONewLoadSeg(a6)
  138.                     move.l    (sp)+,d2
  139.                     lea       VM_ManagerTags(pc),a0
  140.                     move.l    d0,ti_Data(a0)
  141.                     move.l    d0,VM_HNDL_SEGLIST(a5)
  142.                     beq       InitFailure
  143.                     move.l    a0,d1
  144.                     jsr       _LVOCreateNewProc(a6)
  145.                     tst.l     d0
  146.                     beq       InitFailure
  147.                     clr.l     VM_HNDL_SEGLIST(a5)
  148.  
  149.                     * Send startup message to VM_Manager
  150.                     move.l    4,a6
  151.                     move.l    VM_INIT_MSG(a5),a1
  152.                     clr.l     VMM_StartupParams(a1)
  153.                     move.w    #VMCMD_Startup,VMM_VMCommand(a1)
  154.                     clr.w     VMM_ReplySignal(a1)
  155.                     move.l    d0,a0
  156.                     adda.w    #TC_SIZE,a0
  157.                     jsr       _LVOPutMsg(a6)
  158.                     clr.l     VM_INIT_MSG(a5)
  159.                     
  160.                     move.l    VM_STARTERPORT(a5),a0
  161.                     move.b    MP_SIGBIT(a0),d1
  162.                     moveq     #1,d0
  163.                     lsl.l     d1,d0
  164.                     jsr       _LVOWait(a6)
  165.  
  166.                     move.l    VM_STARTERPORT(a5),a0
  167.                     jsr       _LVOGetMsg(a6)
  168.                     move.l    d0,VM_INIT_MSG(a5)
  169.                     beq       InitFailure
  170.                     move.l    d0,a1
  171.                     cmp.w     #VMCMD_InitReady,VMM_VMCommand(a1)
  172.                     bne       InitFailure
  173.                     bra       GotInitMsg                    
  174.  
  175. FoundPort:          * Allocate a request msg to send to the VM_Manager
  176.                     moveq     #VMM_SIZE,d0
  177.                     move.l    #MEMF_PUBLIC|MEMF_CLEAR,d1
  178.                     jsr       _LVOAllocMem(a6)
  179.                     move.l    d0,VM_INIT_MSG(a5)
  180.                     beq       InitFailure
  181.                     move.l    d0,a1
  182.                     move.w    #VMCMD_ReqMemHeader,VMM_VMCommand(a1)
  183.                     move.l    ThisTask(a6),d0
  184.                     move.l    d0,VMM_VMSender(a1)
  185.                     move.l    VM_STARTERPORT(a5),a0
  186.                     move.b    MP_SIGBIT(a0),d0
  187.                     ext.w     d0
  188.                     move.w    d0,VMM_ReplySignal(a1)
  189.                     move.l    VM_PORT(a5),a0
  190.                     jsr       _LVOPutMsg(a6)
  191.  
  192.                     move.l    VM_STARTERPORT(a5),a0
  193.                     move.b    MP_SIGBIT(a0),d1
  194.                     moveq     #1,d0
  195.                     lsl.l     d1,d0
  196.                     jsr       _LVOWait(a6)
  197.  
  198. GotInitMsg:         move.l    VM_INIT_MSG(a5),a1
  199.                     move.l    VMM_VMSema(a1),VM_SEMA(a5)
  200.                     move.l    VMM_VMHeader(a1),VM_MEMHEADER(a5)
  201.  
  202.                     bsr       Cleanup
  203.                     move.l    a5,d0
  204.                     move.l    (sp)+,a5
  205.                     rts
  206.  
  207. InitFailure:        bsr       Cleanup
  208.                     move.l    (sp)+,a5
  209.                     moveq     #0,d0
  210.                     rts
  211.  
  212. Cleanup:            move.l    a6,-(sp)
  213.                     move.l    4,a6
  214.                     move.l    VM_STARTERPORT(a5),a1
  215.                     tst.l     a1
  216.                     beq       NoPort
  217.                     jsr       _LVORemPort(a6)
  218.                     move.l    VM_STARTERPORT(a5),a0
  219.                     jsr       _LVODeleteMsgPort(a6)
  220.  
  221. NoPort:             move.l    VM_INIT_MSG(a5),a1
  222.                     tst.l     a1
  223.                     beq       NoMsg
  224.                     moveq     #VMM_SIZE,d0
  225.                     jsr       _LVOFreeMem(a6)
  226.  
  227. NoMsg:              move.l    VM_HNDL_SEGLIST(a5),d1
  228.                     beq       NoSegList
  229.                     move.l    VM_DOSLIB(a5),a6
  230.                     jsr       _LVOUnLoadSeg(a6)
  231.                     move.l    4,a6
  232.  
  233. NoSegList:          move.l    VM_DOSLIB(a5),a1
  234.                     jsr       _LVOCloseLibrary(a6)
  235.                     move.l    (sp)+,a6
  236.                     rts
  237.  
  238. *********************************************************************
  239.  
  240. Open:
  241.                     add.w     #1,LIB_OPENCNT(a6)
  242.                     bclr.b    #LIBB_DELEXP,LIB_FLAGS(a6)
  243.                     move.l    a6,d0
  244.                     rts                                                 
  245.  
  246. *********************************************************************
  247.  
  248. Close:              moveq     #0,d0
  249.                     sub.w     #1,LIB_OPENCNT(a6)
  250.                     bne       CloseReady
  251.                     btst.b    #LIBB_DELEXP,LIB_FLAGS(a6)
  252.                     beq       CloseReady
  253.                     bsr       Expunge
  254. CloseReady          rts
  255.  
  256. *********************************************************************
  257.  
  258.                     * Expunge not possible for now
  259.  
  260. Expunge:            tst.w     LIB_OPENCNT(a6)
  261.                     beq       DoExpunge
  262.                     bset.b    #LIBB_DELEXP,LIB_FLAGS(a6)
  263.                     moveq     #0,d0
  264.                     rts
  265.  
  266. DoExpunge           movem.l   d2/a5,-(sp)
  267.                     move.l    a6,a5
  268.                     move.l    4,a6
  269.                     move.l    VM_LIB_SEGLIST(a5),d2
  270.                     * Remove from library list
  271.                     move.l    a5,a1
  272.                     jsr       _LVORemove(a6)
  273.                     * Free the library's memory
  274.                     moveq     #0,d0
  275.                     move.l    a5,a1
  276.                     move.w    LIB_NEGSIZE(a5),d0
  277.                     suba.w    d0,a1
  278.                     add.w     LIB_POSSIZE(a5),d0
  279.                     jsr       _LVOFreeMem(a6)
  280.                     move.l    d2,d0
  281.                     move.l    a5,a6
  282.                     movem.l   (sp)+,d2/a5
  283.                     rts
  284.  
  285. *********************************************************************
  286.  
  287. Null:               moveq     #0,d0
  288.                     rts
  289.       
  290. *********************************************************************
  291.  
  292. AllocVMem           * inputs:
  293.                     *   d0 : requested size
  294.                     *   d1 : attributes (only MEMF_CLEAR supported)
  295.                     *   a6 : VMMBase
  296.                     * outputs:
  297.                     *   d0 : pointer to allocated memory or NULL
  298.  
  299.                     movem.l   d0-d1/a2,-(sp)
  300.                     move.l    4,a2
  301.                     exg       a2,a6
  302.                     move.l    VM_SEMA(a2),a0
  303.                     jsr       _LVOObtainSemaphore(a6)  * Does not change registers
  304.                     move.l    VM_MEMHEADER(a2),a0
  305.                     jsr       _LVOAllocate(a6)
  306.                     move.l    VM_SEMA(a2),a0
  307.                     jsr       _LVOReleaseSemaphore(a6)
  308.                     exg       a2,a6
  309.  
  310.                     move.l    d0,a0
  311.                     movem.l   (sp)+,d0-d1
  312.                     btst.l    #MEMB_CLEAR,d1
  313.                     beq       NoClear
  314.  
  315.                     addq.l    #7,d0                    * Align to 8-byte boundary                
  316.                     and.w     #$fff8,d0
  317.                     move.l    a0,a2
  318.                     adda.l    d0,a2                    * end pointer
  319.                     move.l    a0,a1
  320.                     moveq     #0,d1
  321. ClearLoop           move.l    d1,(a1)+
  322.                     cmpa.l    a1,a2
  323.                     bne       ClearLoop
  324.  
  325. NoClear             move.l    (sp)+,a2
  326.                     move.l    a0,d0
  327.                     rts
  328.  
  329. *********************************************************************
  330.  
  331. FreeVMem            * inputs:
  332.                     *   a1 : memory to be freed
  333.                     *   d0 : size of block
  334.                     *   a6 : VMMBase
  335.                     move.l    a2,-(sp)
  336.                     move.l    4,a2
  337.                     exg       a2,a6
  338.                     move.l    VM_SEMA(a2),a0
  339.                     jsr       _LVOObtainSemaphore(a6)  * Does not change registers
  340.                     move.l    VM_MEMHEADER(a2),a0
  341.                     jsr       _LVODeallocate(a6)
  342.                     move.l    VM_SEMA(a2),a0
  343.                     jsr       _LVOReleaseSemaphore(a6)
  344.                     exg       a2,a6
  345.                     move.l    (sp)+,a2
  346.                     rts
  347.  
  348. *********************************************************************
  349.  
  350. AvailVMem           * inputs:
  351.                     *   d1 : attributes (only MEMF_LARGEST valid)
  352.                     *   a6 : VMMBase
  353.                     * outputs:
  354.                     *   d0: size
  355.  
  356.                     move.l    a2,-(sp)
  357.                     move.l    4,a2
  358.                     exg       a2,a6
  359.                     move.l    VM_SEMA(a2),a0
  360.                     jsr       _LVOObtainSemaphore(a6)
  361.                     move.l    VM_MEMHEADER(a2),a0
  362.                     btst.l    #MEMB_LARGEST,d1
  363.                     bne       CheckLargest
  364.                     move.l    MH_FREE(a0),d0
  365.                     bra       SizeFound
  366.  
  367. CheckLargest:       move.l    MH_FIRST(a0),a0
  368.                     moveq     #0,d0
  369.  
  370. LoopChunks          tst.l     a0
  371.                     beq       SizeFound
  372.                     cmp.l     MC_BYTES(a0),d0
  373.                     bpl       SmallerChunk
  374.                     move.l    MC_BYTES(a0),d0
  375. SmallerChunk        move.l    MC_NEXT(a0),a0
  376.                     bra       LoopChunks
  377.  
  378. SizeFound:          move.l    VM_SEMA(a2),a0
  379.                     jsr       _LVOReleaseSemaphore(a6)
  380.                     exg       a2,a6
  381.                     move.l    (sp)+,a2
  382.                     rts
  383.  
  384. *********************************************************************
  385.  
  386. AllocVVec           * inputs:
  387.                     *   d0 : requested size
  388.                     *   d1 : attributes (only MEMF_CLEAR supported)
  389.                     *   a6 : VMMBase
  390.                     * outputs:
  391.                     *   d0 : pointer to allocated memory or NULL
  392.  
  393.                     add.l     #4,d0
  394.                     move.l    d0,-(sp)
  395.                     bsr       AllocVMem
  396.                     move.l    d0,a0
  397.                     tst.l     d0
  398.                     beq       NoMem
  399.                     move.l    (sp)+,(a0)
  400.                     addq.l    #4,d0
  401.                     rts
  402.  
  403. NoMem:              add.w     #4,sp
  404.                     rts
  405.  
  406. *********************************************************************
  407.  
  408. FreeVVec            * inputs:
  409.                     *   a1 : memory to be freed
  410.                     *   a6 : VMMBase
  411.  
  412.                     tst.l     a1
  413.                     beq       NoFree
  414.                     sub.w     #4,a1
  415.                     move.l    (a1),d0
  416.                     bra       FreeVMem
  417.  
  418. NoFree              rts
  419.  
  420. EndCode
  421.                     end
  422.